package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.RegisterUserReq;
import net.ib.mntalk.protocol.Session.RegisterUserRes;
import net.ib.mntalk.protocol.Session.RemoveMessagesRes;
import net.ib.mntalk.protocol.Session.UnRegisterUserReq;
import net.ib.mntalk.protocol.Session.UnRegisterUserRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.ChatDatabaseHelper;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class UnRegisterUserTask extends AsyncNetTask{
	private UnRegisterUserReq mReq;
	
	public UnRegisterUserTask(Context context, Handler handler, UnRegisterUserReq req) {
		super(context, handler);
		mReq = req;
	}

	protected TcpEnvelope onPreExecute() {
		TcpEnvelope revelop = new TcpEnvelope.Builder()
		.setMethodId(Session.Registration.UnRegisterUser)
		.setMessage(mReq)
		.setType(TcpEnvelope.REQUEST)
		.build();
		MTalkLogWriter.d("UnRegisterUserTask.onPreExecute()");
		return revelop;
	}
	
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("UnRegisterUserTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		UnRegisterUserRes unRegisterUserRes = (UnRegisterUserRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("UnRegisterUserTask ResponseCode : " + unRegisterUserRes.getResponseCode());
		
		if(unRegisterUserRes.getResponseCode() == Common.SUCCESS){
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNREGISTER_USER_SUCCESS));
		} else{
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNREGISTER_USER_FAIL));
		}
	}
}
